<template>
    <Table stripe :columns="columns" :data="data"></Table>
</template>
<script>
    export default {
        data () {
            return {
                columns: [
                    {
                        title: '标题',
                        key: 'title',
                        sortable: true
                    },
                    {
                        title: '分类',
                        key: 'tags',
                        sortable: true
                    },
                    {
                        title:'操作',
                        key:'readingAmount',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'info',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.$router.push(this.$route.path+'/'+params.row._id);
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            console.log(params.row._id);
                                            this.$Modal.confirm({
                                                title: '确认删除',
                                                content: '<p>删除 <strong>'+params.row.title+'</strong> ?</p>',
                                                loading: true,
                                                onOk: () => {
                                                    console.log(params.row._id);
                                                    const that = this;
                                                    this.$http.get(
                                                            that.surl+'deletedraft?sessionID='+that.$cookie.get('sessionID')+'&_id='+params.row._id
                                                        )
                                                        .then(function (response) {
                                                            console.log(response.data);
                                                            if(response.data===true){
                                                                that.$Modal.remove();
                                                                console.log(that.data[params.index].title);
                                                                console.log(that.data);
                                                                that.data.splice(params.index,1);
                                                                that.$Message.success('删除成功');
                                                            }else{
                                                                that.$Modal.remove();
                                                                that.$Message.error('删除失败');
                                                            }
                                                        })
                                                        .catch(function (error) {
                                                            that.$Modal.remove();
                                                            that.$Message.error('删除失败');
                                                        });
                                                }
                                            });
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }                    
                ],
                data: [

                ]
            }
        },
        mounted(){
            if(this.$cookie.get('sessionID')){
                this.$Loading.start();//加载动画开始
                const that = this;
                this.$http.get(
                that.surl+'drafts?sessionID='+that.$cookie.get('sessionID')
            )
          .then(function (response) {
            that.$Loading.finish();//加载完毕
            if(response.data!==false){
              that.data=response.data;
            }else{
                that.$router.push('/admin/404');
            }
          })
          .catch(function (error) {
            that.$router.push('/admin/404');
          });
    }
        }
    }
</script>